<?php
class c_pages extends CI_Controller{
    
    private $data;
    
    public function __construct(){
        parent::__construct();
        $this->load->helper("url");
        $this->load->model('m_model');
    }
    
    public function index(){
        $this->data["page_title"] = "Trang chủ";
        $this->data["page_left"] = "v_pages";
        $this->data["page_right"] = "v_right_side_home";

        $newsId = $this->uri->segment(2);
        $this->renderRightBar();
        $this->renderRelate($newsId);
        $newsDetail = $this->renderContent($newsId);
        
        $currentView = $newsDetail[0]->count_view;
        $view = $currentView + 1;
        $this->m_model->countViewByNewsId($newsId, $view);

        $this->data["content"] = $newsDetail;
        $this->load->view("v_masterpage.php", $this->data);
    }

    private function renderContent($newsId){
        $result = $this->m_model->getNewsById($newsId);
        return $result;
    }

    private function renderRightBar(){
        $bestNews = $this->m_model->getBestNewsRead();
        $this->data["listBestNews"] = $bestNews;
        
        $bestVideo = $this->m_model->getBestViewVideo();
        $this->data["listBestVideo"] = $bestVideo;
        
        $tagList = $this->m_model->get_tag_list(10);
        $this->data["tagList"] = $tagList;
    }
    
    private function renderRelate($currentNewsId){
        $relateNews = $this->m_model->getRelateNews($currentNewsId);
        $this->data["listRelateNews"] = $relateNews;
    }
    
}